home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / intrvews / xgrab.lha / xgrab / ui / dview.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-04-25  |  10.7 KB  |  475 lines

  1. /**
  2.    GRAB Graph Layout and Browser System
  3.  
  4.    Copyright (c) 1989, Tera Computer Company
  5.  **/
  6.  
  7. /**
  8.    Implementation of built-in attribute display
  9.  **/
  10.  
  11. #include "istring.h"
  12. #include "dview.h"
  13. #include <InterViews/pattern.h>
  14. #include <InterViews/event.h>
  15. #include <InterViews/glue.h>
  16. #include <InterViews/shape.h>
  17. #include <InterViews/sensor.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20.  
  21. static char* colorst[numcolor] = 
  22. {
  23.     "black       ",
  24.     "gray        ",
  25.     "white       "
  26. };
  27.  
  28. static char* brushst[numbrush] = 
  29. {
  30.     "solid       ",
  31.     "bold solid  ",
  32.     "dotted      ",
  33.     "bold dotted ",
  34.     "dashed      ",
  35.     "bold dashed "
  36. };
  37.  
  38. static char* shapest[numshape] = 
  39. {
  40.     "rectangle   ",
  41.     "circle      ",
  42.     "diamond     ",
  43.     "oval        ",
  44.     "point       ",
  45.     "double_box  ",
  46.     "error       "        /* a line shouldn't show up */
  47. };
  48.  
  49. static char* no_color_str = "no color    ";
  50. static char* no_brush_str = "no brush    ";
  51. static char* no_shape_str = "no shape    ";
  52.  
  53. DrawView::DrawView (int n)
  54.   /**
  55.      The display consists of a depiction of the 3 attributes and 3 buttons 
  56.      to change the values of the attributes
  57.    **/
  58. {
  59.     size = n;
  60.  
  61.     currentColor = default_color;
  62.     currentBrush = default_brush;
  63.     colorset = false;
  64.     brushset = false;
  65.     shapeset = false;
  66.  
  67.     colorButton = new CycleParam("color", colorString(), this, colort);
  68.     brushButton = new CycleParam("brush", brushString(), this, brusht);
  69.     shapeButton = new CycleParam("shape", shapeString(), this, shapet);
  70.  
  71.     depiction = new Depiction();
  72.     
  73.     Shape *s = depiction->GetShape();
  74.     s->width = size;
  75.     s->height = size/2;
  76.     s->hshrink = 0;    /* so the panner can't expand */
  77.     s->hstretch = 0;
  78.     s->vshrink = vfil;   /* expanding vertiaclly's's okay */
  79.     s->vstretch = vfil;
  80.     depiction->Reshape(*s);
  81.  
  82.     depiction->painter = new Painter();
  83.     depiction->painter->FillBg(true);
  84.     depiction->painter->SetColors(palette[currentColor], white);
  85.     depiction->painter->SetPattern(solid);
  86.     depiction->painter->SetBrush(brushes[currentBrush]);
  87.     depiction->currShape = default_shape;
  88.  
  89.     Insert(depiction);
  90.     Insert(new VGlue(10, 0, 0));
  91.     Insert(shapeButton);
  92.     Insert(brushButton);
  93.     Insert(colorButton);
  94.     DrawDep ();
  95. }
  96.  
  97.   /* drawing, redrawing, etc. routines.   Must always redraw the depiction */
  98.  
  99. void DrawView::Draw()
  100. {
  101.     VBox::Draw();
  102.  
  103.     if (depiction->canvas != nil)
  104.     {
  105.     output->ClearRect(depiction->canvas, 0, 0, 
  106.               depiction->xmax, depiction->ymax);
  107.     DrawDep();
  108.     }
  109. }
  110.  
  111. void DrawView::Redraw (Coord a, Coord b, Coord c, Coord d)
  112. {
  113.     Draw();
  114. }
  115.  
  116. void DrawView::Update()
  117. {
  118.     Draw();
  119. }
  120.  
  121. void DrawView::Resize ()
  122. {
  123.     VBox::Resize();
  124.  
  125.     if (depiction->canvas != nil)
  126.     {
  127.         DrawDep();
  128.     }
  129. }
  130.  
  131. void DrawView::EraseDep ()
  132.   /* set the colors to white and draw it */
  133. {
  134.     depiction->painter->SetColors(white, white);
  135.     DrawDep();
  136.     depiction->painter->SetColors(palette[currentColor], white);
  137. }
  138.  
  139. void DrawView::DrawDep ()
  140. {
  141.     depiction->Draw();
  142. }
  143.     
  144. char* DrawView::colorString()
  145.   /* return the string representing the current color */
  146. {
  147.     if (colorset)
  148.     {
  149.     return colorst[currentColor];
  150.     }
  151.     else
  152.     {
  153.     return no_color_str;
  154.     }
  155. }
  156.  
  157. char* DrawView::brushString()
  158.   /* return the string representing the current brush */
  159. {
  160.     if (brushset)
  161.     {
  162.     return brushst[currentBrush];
  163.     }
  164.     else
  165.     {
  166.     return no_brush_str;
  167.     }
  168. }
  169.  
  170. char* DrawView::shapeString()
  171.   /* return the string representing the current shape */
  172. {
  173.     if (shapeset)
  174.     {
  175.     return shapest[depiction->currShape];
  176.     }
  177.     else
  178.     {
  179.     return no_shape_str;
  180.     }
  181. }
  182.  
  183. void DrawView::reset(AttType type)
  184.   /**
  185.      reset the attribute of type type to its default value.  Called by
  186.      the CycleParam objects
  187.    **/
  188. {
  189.     EraseDep();
  190.     depiction->inhibit();  
  191.       /**
  192.      if we don't do this, each time we change the button text
  193.      the depiction will be redrawn
  194.        **/
  195.  
  196.     switch (type)
  197.     {
  198.     case shapet:
  199.         shapeset = false;
  200.         depiction->currShape = default_shape;
  201.         shapeButton->ChangeText(shapeString());
  202.         break;
  203.  
  204.     case colort:
  205.         colorset = false;
  206.         currentColor = default_color;
  207.         colorButton->ChangeText(colorString());
  208.             depiction->painter->SetColors(palette[currentColor], white);
  209.         break;
  210.  
  211.     case brusht:
  212.         brushset = false;
  213.         currentBrush = default_brush;
  214.         brushButton->ChangeText(brushString());
  215.             depiction->painter->SetBrush(brushes[currentBrush]);
  216.         break;
  217.     
  218.     }
  219.  
  220.     depiction->allow();
  221.     DrawDep();
  222. }
  223.  
  224.  
  225. void DrawView::cycle(char* text, boolean forward, AttType type)
  226.   /**
  227.      cycle through the attribute of type type either forward or
  228.      backward.  text is the current value of the attribute.  Called 
  229.      by the CycleParam objects
  230.    **/
  231. {
  232.     int i;
  233.  
  234.     EraseDep();
  235.     depiction->inhibit();
  236.       /* Don't redraw the depiction when the button text is changed */
  237.  
  238.     switch (type)
  239.     {
  240.     case shapet:
  241.             if (!strcmp(text, no_shape_str))   /* currently no shape */
  242.             {
  243.             shapeset = true;
  244.             depiction->currShape = forward ? rectangle : numshape - 2;
  245.             }
  246.         else   /* find the current shape */
  247.         {
  248.             for (i = 0; i < numshape; i++)
  249.                 {
  250.                 if (!strcmp(shapest[i], text))
  251.                 {
  252.                     if ((forward && i == numshape - 2) ||
  253.                     (!forward && i == rectangle))
  254.               /* changing to noshape */
  255.                     {
  256.                     depiction->currShape = default_shape;
  257.                     shapeset = false;
  258.                     }
  259.                     else
  260.                     {
  261.                     depiction->currShape = forward ? 
  262.                   NextShape(depiction->currShape) :
  263.                   PrevShape(depiction->currShape);
  264.                     }
  265.  
  266.                 break;
  267.                 }
  268.                 }
  269.         }
  270.  
  271.         shapeButton->ChangeText(shapeString());
  272.         break;
  273.  
  274.     case colort:
  275.             if (!strcmp(text, no_color_str))    /* currently no color */
  276.             {
  277.             colorset = true;
  278.             currentColor = forward ? blackc : numcolor - 1;
  279.         }
  280.         else    /* find the current color */
  281.         {
  282.                 for (i = 0; i < numcolor; i++)
  283.                 {
  284.                 if (!strcmp(colorst[i], text))
  285.                 {
  286.                     if ((forward && i == numcolor - 1) ||
  287.                     (!forward && i == blackc))
  288.               /* changing to no color */
  289.                     {
  290.                     currentColor = default_color;
  291.                     colorset = false;
  292.                     }
  293.                     else
  294.                     {
  295.                     currentColor = forward ? NextColor(currentColor) :
  296.                                      PrevColor(currentColor);
  297.                     }
  298.             
  299.                 break;
  300.                 }
  301.                 }
  302.         }
  303.  
  304.         colorButton->ChangeText(colorString());
  305.             depiction->painter->SetColors(palette[currentColor], white);
  306.         break;
  307.  
  308.     case brusht:
  309.             if (!strcmp(text, no_brush_str))    /* currently no brush */
  310.             {
  311.             brushset = true;
  312.             currentBrush = forward ? solidb : numbrush - 1;
  313.             }
  314.         else        /* find the current brush */
  315.         {
  316.                 for (i = 0; i < numbrush; i++)
  317.                 {
  318.                 if (!strcmp(brushst[i], text))
  319.                 {
  320.                     if ((forward && i == numbrush - 1) ||
  321.                     (!forward && i == solidb))
  322.               /* change to no brush */
  323.                     {
  324.                     currentBrush = default_brush;
  325.                     brushset = false;
  326.                     }
  327.                     else
  328.                     {
  329.                     currentBrush = forward ? NextBrush(currentBrush) :
  330.                                  PrevBrush(currentBrush);
  331.                     }
  332.             
  333.                     break;
  334.                 }
  335.                 }
  336.         }
  337.  
  338.         brushButton->ChangeText(brushString());
  339.             depiction->painter->SetBrush(brushes[currentBrush]);
  340.         break;
  341.     
  342.     }
  343.  
  344.     depiction->allow();
  345.     DrawDep();
  346. }
  347.  
  348. CycleParam::CycleParam(char* btext, char* mtext, DrawView* d, AttType t)
  349.   /**
  350.      a button with text inside and text outside.
  351.      Need to know the drawview to call routines when the button is pressed.
  352.      Pass AttType to drawview so it knows which button we are
  353.    **/
  354. {
  355.     dv = d;
  356.     text = mtext;
  357.     type = t;
  358.     message = new Message(strdup(text));
  359.     button = new CycleButton(btext, new ButtonState(), this);
  360.  
  361.     Insert(button);
  362.     Insert(new HGlue);
  363.     Insert(message);
  364. }
  365.  
  366. void CycleParam::ChangeText (char* t)
  367.   /* change the text *outside* the button */
  368. {
  369.     Remove(message);
  370.     delete message;
  371.     text = t;
  372.     message = new Message(strdup(t));
  373.     Insert(message);
  374.     Change(message);
  375. }
  376.  
  377. void CycleParam::reset()
  378.   /* reset the attribute the button represents */
  379. {
  380.     dv->reset(type);
  381.  
  382. void CycleParam::cycle(boolean forward)
  383.   /* cycle the attribute the button represents */
  384. {
  385.     dv->cycle(text, forward, type);
  386.  
  387. CycleButton::CycleButton (char* s, ButtonState* b, CycleParam* p) : (s, b, -1)
  388.   /* the button for a cycle param */
  389. {
  390.     cp = p;
  391. }
  392.  
  393. void CycleButton::Handle (Event& e) 
  394.   /**
  395.      right mouse button: cycle forward
  396.      middle mouse button: reset
  397.      left moues button: cycle backward
  398.    **/
  399. {
  400.     if (e.eventType == DownEvent)
  401.     {
  402.     switch (e.button)
  403.     {
  404.         case RIGHTMOUSE:
  405.         cp->cycle(true);
  406.         break;
  407.         case MIDDLEMOUSE:
  408.         cp->reset();
  409.         break;
  410.         case LEFTMOUSE:
  411.         cp->cycle(false);
  412.         break;
  413.     }
  414.     }
  415. }
  416.  
  417. Depiction::Depiction() : ()
  418.   /* the drawview sets most of the variables */
  419. {
  420.     okaytodraw = true;
  421. }
  422.  
  423. void Depiction::Draw()
  424. {
  425.     if (okaytodraw && canvas != nil)
  426.     {
  427.         switch(currShape)
  428.         {
  429.         case circle:
  430.             painter->Circle(canvas, shape->width/2, shape->height/2, 
  431.                 min(shape->width, shape->height) / 4);
  432.             break;
  433.         case oval:
  434.             painter->Ellipse(canvas, shape->width/2, shape->height/2, 
  435.                  shape->width/4, shape->height/4);
  436.             break;
  437.         case npoint:
  438.             painter->Point(canvas, shape->width/2, shape->height/2);
  439.             break;
  440.         case diamond:
  441.             {
  442.                 Coord x[4], y[4];
  443.     
  444.                 x[0] = shape->width/4;      y[0] = shape->height/2;
  445.                 x[1] = shape->width/2;      y[1] = shape->height/4;
  446.                 x[2] = 3 * shape->width/4;  y[2] = shape->height/2;
  447.                 x[3] = shape->width/2;      y[3] = 3 * shape->height/4;
  448.                 painter->Polygon(canvas, x, y, 4);
  449.             }
  450.             break;
  451.         case double_box:
  452.             painter->Rect(canvas, shape->width/4, shape->height/4,
  453.                   3 * shape->width/4, 3 * shape->height/4);
  454.             painter->Rect(canvas, shape->width/4 + db_margin, 
  455.                   shape->height/4 + db_margin,
  456.                   3 * shape->width/4 - db_margin,
  457.                   3 * shape->height/4 - db_margin);
  458.             break;
  459.         case rectangle:
  460.             painter->Rect(canvas, shape->width/4, shape->height/4, 
  461.                   3 * shape->width/4, 3 * shape->height/4);
  462.             break;
  463.         case line:
  464.             break;
  465.         }
  466.     }
  467. }
  468.  
  469. void Depiction::Redraw(Coord a, Coord b, Coord c, Coord d)
  470. {
  471.     Draw();
  472. }
  473.